package cn.glc.daily.life.web.service.captcha.abs;

import cn.glc.daily.life.common.enums.GlobalCodeEnum;
import cn.glc.daily.life.common.exceptions.GlobalException;
import cn.glc.daily.life.model.dto.SendCodeDto;

import java.util.Optional;
import java.util.regex.Pattern;

/**
 * 邮箱发送实现
 *
 * @author simon
 * @date 2020/6/1 16:27
 * @copyright:
 * @modifier
 * @motto a no talent, people who have to study hard
 */
public abstract class AbstractEmailCodeSend extends AbstractSendCode {
    /**
     * 邮箱格式校验
     */
    private final String regexp = "[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?";
    private final Pattern pattern = Pattern.compile(regexp);

    @Override
    protected boolean vail(SendCodeDto sendCode) {
        Optional.ofNullable(sendCode).orElseThrow(() -> new GlobalException(GlobalCodeEnum.BusinessExceptionEnum.MAIL_NOT_EMPTY));
        Optional.ofNullable(sendCode).filter(o -> pattern.matcher(sendCode.getNumber()).matches()).orElseThrow(() -> new GlobalException(GlobalCodeEnum.BusinessExceptionEnum.MAIL_ERROR));
        return true;
    }

    @Override
    protected boolean send(SendCodeDto sendCode) {
        return false;
    }

    @Override
    protected boolean destroy(SendCodeDto sendCode) {
        return false;
    }
}
